@echo off
:: process_monitor.bat - 使用WMIC检测进程状态
setlocal EnableDelayedExpansion

set PYTHON_SCRIPT=ths_stock_dfcf_kafka.py
set CHECK_INTERVAL=10
set MAX_RESTARTS=50
set RESTART_COUNT=0

echo [%time%] 开始监控Python进程: %PYTHON_SCRIPT%

:MONITOR_LOOP
:: 使用WMIC检查特定Python进程是否存在
wmic process where "name='python.exe' and commandline like '%%%PYTHON_SCRIPT%%%'" get processid 2>nul | findstr /r "[0-9][0-9]*" > nul

if errorlevel 1 (
    echo [%time%] 进程已挂掉，重新启动...
    set /a RESTART_COUNT+=1
    echo 重启次数: !RESTART_COUNT!
    
    :: 启动Python进程
    start "Python_Process" /min python "%PYTHON_SCRIPT%" sh uat 1
    
    :: 检查重启次数限制
    if !RESTART_COUNT! gtr %MAX_RESTARTS% (
        echo [%time%] 达到最大重启次数，停止监控
        pause
        exit /b 1
    )
) else (
    echo [%time%] 进程运行正常
)

timeout /t %CHECK_INTERVAL% /nobreak > nul
goto MONITOR_LOOP